package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.bean.BeanValidators;
import com.ruoyi.system.api.domain.SysStudent;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.manager.SysStudentManager;
import com.ruoyi.system.service.ISysStudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Validator;
import java.util.List;

/**
 * @author jlc
 * @date 2022/9/4
 */
@Slf4j
@Service
public class SysStudentServiceImpl implements ISysStudentService {


    @Autowired
    private SysStudentManager studentManager;

    @Autowired
    protected Validator validator;

    @Override
    public List<SysStudent> selectStudentList(SysStudent user) {

        // 查询学生列表所有数据， 包括学生属于那个部门的
        return studentManager.queryList(user);
    }


    /**
     * 导入用户数据
     *
     * @param studentList 用户数据列表
     * @param updateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importStudent(List<SysStudent> studentList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(studentList) || studentList.size() == 0) {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (SysStudent student : studentList) {
            try {
                // 验证是否存在这个人
                SysStudent u = studentManager.getByIdCard(student.getIdCard());
                if (StringUtils.isNull(u)) {
                    BeanValidators.validateWithException(validator, student);
                    student.setCreateBy(operName);
                    studentManager.save(student);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、学生 " + student.getName() + " 导入成功");
                } else if (updateSupport) {
                    BeanValidators.validateWithException(validator, student);
                    student.setUpdateBy(operName);
                    studentManager.save(student);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、学生 " + student.getName() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、学生 " + student.getName() + " 已存在");
                }
            }
            catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、学生 " + student.getName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
